import 'package:boss/ui/page/analysis/provider/analysis_provider.dart';
import 'package:boss/ui/page/analysis/widget/time_row.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'revenue_data_card.dart';
import 'revenue_offer_table.dart';
import 'revenue_pie.dart';

class RevenueBrowser extends StatefulWidget {
  @override
  _RevenueBrowserState createState() => _RevenueBrowserState();
}

class _RevenueBrowserState extends State<RevenueBrowser> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Consumer<AnalysisProvider>(
      builder: (context, provider, _) {
        return LoaderPullRefresh(
          refreshTask: Provider.of<AnalysisProvider>(
            context,
            listen: false,
          ).queryStoreRevenue,
          builder: (context) {
            return SingleChildScrollView(
              padding: EdgeInsets.all(30.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  TimeRow(provider.timerRangeText),
                  RevenueDataCard(
                    title: '订单金额',
                    titleTip: '统计时间内，店内订单实收金额\r\n于外卖已完成订单商家预计收入之和',
                    centerText: provider.storeRevenue.totalPriceAmount,
                    yesterdayPercentage: provider.storeRevenue.totalPriceAmountPercent,
                    storeDataText: provider.storeRevenue.tangTotalPriceAmount,
                    storePercentage: provider.storeRevenue.tangTotalPriceAmountPercent,
                    takeoutDataText: provider.storeRevenue.outFoodTotalPriceAmount,
                    takeoutPercentage: provider.storeRevenue.outFoodTotalPriceAmountPercent,
                    unit: '¥',
                    type: 0,
                  ),
                  RevenueDataCard(
                    title: '实收金额',
                    titleTip: '统计时间内，店已结账订单实收金额\r\n于外卖已完成订单商家预计收入之和',
                    centerText: provider.storeRevenue.totalAmount,
                    yesterdayPercentage: provider.storeRevenue.totalAmountPercent,
                    storeDataText: provider.storeRevenue.tangFoodAmount,
                    storePercentage: provider.storeRevenue.tangFoodAmountPercent,
                    takeoutDataText: provider.storeRevenue.totalOutFoodAmount,
                    takeoutPercentage: provider.storeRevenue.totalOutFoodAmountPercent,
                    unit: '¥',
                    type: 0,
                  ),
                  RevenueDataCard(
                    title: '订单数',
                    titleTip: '统计时间内，所有已结账订单的数量',
                    centerText: provider.storeRevenue.orderNum,
                    yesterdayPercentage: provider.storeRevenue.orderNumPercent,
                    storeDataText: provider.storeRevenue.tangFoodNum,
                    storePercentage: provider.storeRevenue.tangFoodNumPercent,
                    takeoutDataText: provider.storeRevenue.totalOutFoodNum,
                    takeoutPercentage: provider.storeRevenue.totalOutFoodNumPercent,
                    unit: '单',
                    type: 1,
                  ),
                  RevenueDataCard(
                    title: '就餐人数',
                    titleTip: '统计时间内，堂食及外带（不含外卖）\r\n所有已结账订单的人数',
                    centerText: provider.storeRevenue.foodPeopleNum,
                    yesterdayPercentage: provider.storeRevenue.foodPeopleNumPercent,
                    storeDataText: provider.storeRevenue.tangFoodPeopleNum,
                    unit: '人',
                    type: 1,
                  ),
                  RevenueCouponDataCard(
                    sendCouponCount: provider.storeRevenue.sendCouponCount,
                    usedCouponCount: provider.storeRevenue.usedCouponCount,
                    usedCouponCountStoreDineCount: provider.storeRevenue.usedCouponCountStoreDineCount,
                    usedCouponCountTakeOutCount: provider.storeRevenue.usedCouponCountTakeOutCount,
                    usedCouponCountStoreDinePercent: provider.storeRevenue.usedCouponCountStoreDinePercent,
                    usedCouponCountTakeOutPercent: provider.storeRevenue.usedCouponCountTakeOutPercent,
                  ),
                  RevenuePie(
                    title: '就餐类型占比',
                    data: provider.moneyData,
                    action: ['按金额', '按单数'],
                    defaultSelectedIndex: provider.moneyType,
                    onActionTap: (index) {
                      provider.setMoneyType(index);
                    },
                    showLabelIndex: provider.moneyIndex,
                    onLabelSelectedChanged: provider.setMoneyIndex,
                  ),
                  RevenuePie(
                    title: '订单来源占比',
                    data: provider.orderData,
                    action: ['按金额', '按单数'],
                    defaultSelectedIndex: provider.orderType,
                    onActionTap: (index) {
                      provider.setOrderType(index);
                    },
                    showLabelIndex: provider.orderIndex,
                    onLabelSelectedChanged: provider.setOrderIndex,
                  ),
                  RevenuePie(
                    title: '店内收款构成',
                    data: provider.payData,
                    showLabelIndex: provider.payIndex,
                    onLabelSelectedChanged: provider.setPayIndex,
                  ),
                  RevenueOfferTable(),
                ],
              ),
            );
          },
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
